const path = require("path")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const TestPlugin = require("./plugins/test-plugin")
const BannerWebpackPlugin = require("./plugins/banner-webpack-plugin")
const AnalyzeWebpackPlugin = require("./plugins/analyze-webpack-plugin")

module.exports = {
    entry: "./src/main.js",
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: 'js/[name].js',
        clean: true
    },
    module: {
        rules: [
            //{
            //test: /\.js$/,
            // 执行顺序： 从下至上，从右至左
            //use: ["./loaders/sync-loader.js", "./loaders/raw-loader.js", "./loaders/async-loader.js", "./loaders/sync-loader.js"]
            // pitch loader 执行顺序： 
            // - pitch 函数先顺序执行，执行完之后方执行 loader 函数。
            // - pitch 函数 从左向右，pitch loader 从右向左。
            // - 如某个 pitch 函数有返回值，则发生熔断，开始从有向左执行已执行 pitch 函数的 loader(不包括有返回值的那个 pitch 函数对应的 loader)
            //use: ["./loaders/pitch-loader3.js", "./loaders/pitch-loader2.js", "./loaders/pitch-loader1.js"]
            //}
            // {
            //     test: /\.js$/,
            //     loader: "./loaders/test-loader.js"
            // }
            {
                test: /\.js$/,
                use: [
                    "./loaders/clear-log-loader.js",
                    // {
                    //     loader: "./loaders/banner-loader",
                    //     options: {
                    //         author: 'Jianshu Shi'
                    //     }
                    // }, 
                    {
                        loader: "./loaders/babel-loader",
                        options: {
                            presets: ["@babel/preset-env"]
                        }
                    }]
            },
            {
                test: /\.(png|jpe?g|gif)$/,
                loader: "./loaders/file-loader",
                type: "javascript/auto", // 解决图片重复打包问题
            }
            ,
            {
                test: /\.css$/,
                use: ["./loaders/style-loader", "css-loader"]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, "./public/index.html")
        }),
        new TestPlugin(),
        new BannerWebpackPlugin({
            authorName: 'damiaoxi'
        }),
        new AnalyzeWebpackPlugin()
    ],
    optimization: {
        splitChunks: {
            chunks: "all"
        },
        runtimeChunk: {

        }
    },
    mode: "production"
}